
package bsc.war.form;

import bsc.ejb.entity.SRM;
import bsc.ejb.entity.SRMTopic;
import bsc.ejb.entity.ScoreCard;
import bsc.ejb.entity.Topic;
import bsc.ejb.entity.TopicScoreView;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.BSCUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author memozac
 */
public class NewSRMTopic extends AbstractController {
   @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
       Long idSRM = null;
       try {
           DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
           
           idSRM = Long.parseLong(request.getParameter("srm"));
           String[] topics = request.getParameterValues("topic");
           String[] scoreCards = request.getParameterValues("scoreCard");
           String[] newTopicNames = request.getParameterValues("newTopicName");
           String[] newTopicDescriptions = request.getParameterValues("newTopicDescription");
           String[] newTopicResponsables = request.getParameterValues("newTopicResponsable");
           String[] newTopicExpectedTimes = request.getParameterValues("newTopicExpectedTime");

           SRM SRM = delegate.findSRM(idSRM);

           if( topics != null )
           for (String idTopic : topics) {
               SRMTopic SRMTopic = (SRMTopic) Factory.getBean("SRMTopic");
               Topic topic = delegate.findTopic(Long.parseLong(idTopic));


               TopicScoreView tsv = delegate.findTopicScoreView(topic, SRM.getScoreView());
               if(tsv == null)
               {
                    tsv = (TopicScoreView) Factory.getBean("TopicScoreView");
                    tsv.setScoreView(SRM.getScoreView());
                    tsv.setTopic(topic);
                    tsv.setType(TopicScoreView.Type.UNINITIALIZE);
                    delegate.createTopicScoreView(tsv);
               }

               SRMTopic.setSRM(SRM);
               SRMTopic.setTopic(topic);
               SRMTopic.setResponsable(request.getParameter("responsable-"+idTopic));
               SRMTopic.setExpectedTime( request.getParameter("expectedTime-"+idTopic) != null && !request.getParameter("expectedTime-"+idTopic).isEmpty() ?
                   Double.parseDouble( request.getParameter("expectedTime-"+idTopic) ) :
                   null );
               delegate.createSRMTopic(SRMTopic);
           }

           if( scoreCards != null )
               for (String idScoreCard : scoreCards) {
                   ScoreCard scoreCard = delegate.findScoreCard(Long.parseLong(idScoreCard));
                   Topic topic = (Topic) Factory.getBean("Topic");
                   topic.setScoreCard(scoreCard);
                   topic.setTopic(scoreCard.getName());
                   delegate.createTopic(topic);
                   
                   TopicScoreView tsv = delegate.findTopicScoreView(topic, SRM.getScoreView());
                   if(tsv == null)
                   {
                        tsv = (TopicScoreView) Factory.getBean("TopicScoreView");
                        tsv.setScoreView(SRM.getScoreView());
                        tsv.setTopic(topic);
                        tsv.setType(TopicScoreView.Type.UNINITIALIZE);
                        delegate.createTopicScoreView(tsv);
                   }

                   SRMTopic SRMTopic = (SRMTopic) Factory.getBean("SRMTopic");
                   SRMTopic.setSRM(SRM);
                   SRMTopic.setDescription(scoreCard.getDescription());
                   SRMTopic.setTopic(topic);
                   SRMTopic.setResponsable(request.getParameter("responsable-"+idScoreCard));
                   SRMTopic.setExpectedTime( request.getParameter("expectedTime-"+idScoreCard) != null && !request.getParameter("expectedTime-"+idScoreCard).isEmpty() ?
                        Double.parseDouble( request.getParameter("expectedTime-"+idScoreCard) ) :
                   null );
                   delegate.createSRMTopic(SRMTopic);
               }

           if( newTopicNames != null && newTopicDescriptions != null )
               for(int i=0; i<newTopicNames.length; i++) {
                   Topic topic = (Topic) Factory.getBean("Topic");
                   topic.setTopic(newTopicNames[i]);
                   delegate.createTopic(topic);

                   SRMTopic SRMTopic = (SRMTopic) Factory.getBean("SRMTopic");
                   SRMTopic.setDescription(newTopicDescriptions[i]);
                   SRMTopic.setExpectedTime(Double.parseDouble( newTopicExpectedTimes[i] ));
                   SRMTopic.setResponsable(newTopicResponsables[i]);

                   TopicScoreView tsv = (TopicScoreView) Factory.getBean("TopicScoreView");
                   tsv.setScoreView(SRM.getScoreView());
                   tsv.setTopic(topic);
                   tsv.setType(TopicScoreView.Type.UNINITIALIZE);
                   delegate.createTopicScoreView(tsv);

                   SRMTopic.setSRM(SRM);
                   SRMTopic.setTopic(topic);
                   delegate.createSRMTopic(SRMTopic);
               }
               
        //SRM.setExpectedTime(BSCUtils.getExpectedTime(delegate, SRM));
        delegate.updateSRM(SRM);

       } catch (Exception e)
       {
            logger.error("Caught exception: ", e);
       }
       
       return new ModelAndView("newSRMTopic");
    }

}
